<?php
/**
 * fconv.class
 *
 * format converter
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex yaroshevich
 * @created:	29.10.2008 13:26:08
 * 
 * @copyright:	(c) 2008 commercial.su. All rights reserved.
 */

/** standart verifying */
//if( !defined( 'COMMERCIAL' ) ) return;

if( defined( '__FCONV.CLASS_PHP' ) ) return;
define( '__FCONV.CLASS_PHP', '0.1' );

class fconv
{
	static private $up = 0;
	static private $ms = null;
	static private $cs = null;
	static private $v = null;
	
	static public function init()
	{
		if( self::$up ) return;
		$db = db::__getMe();
		$r = array();
		$_ms = $db ->_list( "select * from core_fconv_measure where usability='main';", 'id' );
		foreach( $_ms as $v )
		{
			// store only selected columns in result
			$r[$v['measure']][$v['units']] = array_intersect_key( $v, array_flip( array( 'id','name','short','koef' ) ) );
		}
		self::$ms = $r;

		$r = array( );
		$_cs = $db ->_list( "select ticker, id, e, rate, name, symbol from core_fconv_currency where rate>0 and symbol is not null;", 'ticker' );
		self::$cs = $_cs;
		
		$v = Visitor::__getMe();
		self::$v = array(
			'language' => 'ru', //$v->getLocale(),
			'measure' => 'metric', //($v->getMeasure()==1)?'metric':'survey',
			'currency' => 'RUB', //($v->getCurrency()==1)?'RUB':'USD'
			);
		
		self::$up ++;
	}
	
	/**
	 *
	 */
	static public function money( $value = 0, $src = null, $dest = null )
	{
		//fb::log( array( 'src' => $src, 'dest' => $dest ), 'in' );
		if( is_null($src) ) $src = "USD";
		if( !isset( self::$cs[ $src ] ) ) $src = "USD";
		if( is_null($dest) || !isset( self::$cs[ $dest ] ) ) $dest = self::$v['currency'];
		//fb::log( self::$cs, 'cs' );
		//fb::log( $src, 'src' );
		//fb::log( $dest, 'dst' );
		
		$r1 = self::$cs[ $src ][ 'rate' ];
		$r2 = self::$cs[ $dest ][ 'rate' ];
		$e = self::$cs[ $dest ][ 'e' ];
		$r = bcmul( $value, bcdiv( $r1, $r2, 8 ), $e );
		/*var_dump( array(
			'val' => $value,
			'src' => $src,
			'dst' => $dest,
			'rsrc' => $r1,
			'rdst' => $r2,
			'result' => $r
			) );*/
		
		return $r;
	}


	static public function moneyunits( $currency = null, $full = false )
	{
		if( is_null($currency) || !isset( self::$cs[ $currency ] ) )
			$currency = self::$v['currency'];
		
		return self::$cs[ $currency ][ $full ? 'name' : 'symbol' ];
	}
        
	/**
	 *
	 */
	static public function date( $tm = 0, $dest_format = null )
	{
		$r = "";
		
		return $r;
	}



	/**
	 * basis
	 */
	static private function parseMeasure( $m, $d = 'metric' )
	{
		return (!is_null($m) && isset(self::$ms['area'][$m])) ? $m : $d;
	}
	
	static private function getValue( $units, $value, $src = null, $dest = null )
	{
		$src = self::parseMeasure( $src );
		$dest = self::parseMeasure( $dest, self::$v['measure'] );
		
		$sk = self::$ms[$units][$src]['koef'];
		$dk = self::$ms[$units][$dest]['koef'];
		
		return bcmul($value,bcdiv($sk,$dk,8),6);
	}

	static private function getUnits( $units, $type = null, $full = false )
	{
		$type = !is_null( $type ) ? $type : self::$v['measure'];
		return self::$ms['area'][$type][$full?'name':'short'];
	}

	/**
	 * wrappers pack
	 */
	static public function length( $value = 0, $src_type = null, $dest_type = null ) {
		return self::getValue( 'length', $value, $src_type, $dest_type ); }
	
	static public function lengthunits( $type = null, $full = false ) {
		return self::getUnits( 'length', $type, $full ); }
	
	static public function area( $value = 0, $src_type = null, $dest_type = null ) {
		return self::getValue( 'area', $value, $src_type, $dest_type ); }
	
	static public function areaunits( $type = null, $full = false ) {
		return self::getUnits( 'area', $type, $full ); }

	static public function landarea( $value = 0, $src_type = null, $dest_type = null ) {
		return self::getValue( 'landarea', $value, $src_type, $dest_type ); }
	
	static public function landareaunits( $type = null, $full = false ) {
		return self::getUnits( 'landarea', $type, $full ); }
	
};



 
?>